import java.util.LinkedList;
import java.util.Queue;

class Solution1765 {
    public int[][] highestPeak(int[][] isWater) {
        int m = isWater.length  , n = isWater[0].length ;
        int[][] arr = new int[m][n] ; 
        boolean[][] hash = new boolean[m][n] ; 
        Queue<int[]> queue = new LinkedList<>() ;
        for(int i=0 ; i < m ; i ++){
            for(int j=0 ; j <n ; j++){
                if(isWater[i][j] == 1){
                    hash [i][j] = true ;
                    queue.add(new int[]{i,j}) ; 
                }
            }
        }
        int[] dx = {0,0,1,-1} ; 
        int[] dy = {1,-1,0,0} ; 
        int len = 0 ; 
        while(!queue.isEmpty()){
            int size = queue.size() ;
            len++ ;
            for(int i=0 ; i < size ;i ++){
              int[] ret = queue.poll() ;
              int a = ret[0] , b = ret[1] ; 
              for(int j=0; j <4 ; j ++){
               int x = dx[j]+a , y = dy[j]+b ; 
               if(x >=0 && x < m && y>=0 && y<n && !hash[x][y]){
                 arr[x][y] = len ; 
                 hash[x][y] = true ;
                 queue.add(new int[]{x , y}) ; 
               }
              }
            }
        }
       return arr ;
    }
}